package first;

import java.util.ArrayList;
import java.util.Scanner;

public class Shop {
    public static void main(String[] args) {
        // 存放商品集合
        ArrayList<Goods> shops = new ArrayList<Goods>();
        while (true) {
            // 请选择操作选项
            int selectFun = selectFunction();
            switch (selectFun) {
                // 货物清单
                case 1:
                    printShopsList(shops);
                    break;
                // 添加新商品
                case 2:
                    addShops(shops);
                    break;
                // 删除货物
                case 3:
                    delShops(shops);
                    break;
                // 修改货物
                case 4:
                    putShops(shops);
                    break;
                // 退出
                case 5:
                    System.out.println("再见");
                    return;
                default:
                    System.out.println("选择有误，请重新选择：");
                    break;
            }
        }
    }

    // 修改货物
    public static void putShops(ArrayList<Goods> shops) {
        Scanner s = new Scanner(System.in);
        System.out.print("请您输入需要删除的商品编号：");
        int id = s.nextInt();
        // 存储数据中是否存在被删除的商品
        boolean flag = false;
        for (int i = 0, len = shops.size(); i < len; i++) {
            Goods goods1 = shops.get(i);
            if (goods1.id == id) {
                goodsPrice(goods1);
                flag = true;
                return;
            }
        }
        if (flag) {
            System.out.print("删除成功");
        } else {
            System.out.print("未找到数据");
        }
    }

    // 删除货物
    public static void delShops(ArrayList<Goods> shops) {
        Scanner s = new Scanner(System.in);
        System.out.print("请您输入需要删除的商品编号：");
        int id = s.nextInt();
        // 存储数据中是否存在被删除的商品
        boolean flag = false;
        for (int i = 0, len = shops.size(); i < len; i++) {
            Goods goods1 = shops.get(i);
            if (goods1.id == id) {
                shops.remove(i);
                --i;
                --len;
                flag = true;
            }
        }
        if (flag) {
            System.out.print("删除成功");
        } else {
            System.out.print("未找到数据");
        }
    }

    // 商品名称和商品价格
    public static void goodsPrice(Goods goods) {
        Scanner s = new Scanner(System.in);
        System.out.println();
        System.out.print("请输入商品名称：");
        String name = s.nextLine();
        System.out.println();
        System.out.print("请输入商品价格：");
        double price = s.nextDouble();
        goods.price = price;
        goods.name = name;
    }

    // 添加新商品
    public static void addShops(ArrayList<Goods> shops) {
        Scanner s = new Scanner(System.in);
        System.out.print("请输入商品编号：");
        int id = s.nextInt();
        Goods goods = new Goods();
        goodsPrice(goods);
        goods.id = id;
        shops.add(goods);
    }

    // 货物清单
    public static void printShopsList(ArrayList<Goods> shops) {
        System.out.println("==============商品库存清单==============");
        System.out.println("商品编号    商品名称    商品价格");
        for (int i = 0; i < shops.size(); i++) {
            Goods goods = shops.get(i);
            System.out.println(goods.id + "    " + goods.name + "    " + goods.price);
        }
    }

    // 请选择操作选项
    public static int selectFunction() {
        System.out.println("请选择操作选项：");
        System.out.println("1. 货物清单");
        System.out.println("2. 添加新商品");
        System.out.println("3. 删除货物");
        System.out.println("4. 修改货物");
        System.out.println("5. 退出");
        Scanner s = new Scanner(System.in);
        return s.nextInt();
    }
}
